from game import zz_game
from zz import zz_log, zz_img

log = zz_log.of()


# 寻道大千

class Game(zz_game.ZZ_Game):
    def __init__(self, d, has_prize=True):
        super().__init__(d, has_prize)

    def get_name(self):
        return "秘钥开箱"

    def play_alert(self):
        if not self.img_exit("./game/img/秘钥开箱/play_alert.png", 2):
            self.log_msg("不存在弹窗")
            return False

        self.log_msg("存在武器弹窗")
        play_state_up = True
        r = self.find_img("./game/img/秘钥开箱/play_state.png", 2)
        if r.ok():
            x, y = r.get_data("first")
            red_color = (173, 78, 74)
            play_state_up = not self.find_color(x, y, red_color)

        if not play_state_up:
            self.log_msg("战力下降，点击出售")
            self.img_click("./game/img/秘钥开箱/play_buy.png")
        else:
            self.log_msg("战力上升，点击收服")
            self.img_click("./game/img/秘钥开箱/play_get.png")

        if self.img_exit("./game/img/秘钥开箱/play_confirm_alert.png", 1):
            self.log_msg("存在弹窗确认，点击确认")
            self.img_click("./game/img/秘钥开箱/play_confirm_1.png", 1)
        return True

    def game_alert(self):
        self.alert_close_by_file("./game/img/秘钥开箱")

    def handle_main(self):
        self.log_msg("等待进入游戏主界面")
        self.img_exit("./game/img/秘钥开箱/game_main.png", 100)
        self.log_msg("已进入游戏主界面")
        self.game_alert()

        if self.not_prize_handle():
            return False

        self.log_msg("开始开宝箱")
        while True:
            for i in range(10):
                self.log_msg(f"{i + 1}-宝箱")
                self.img_click("./game/img/秘钥开箱/play.png")
                self.play_alert()

            if self.check_done():
                self.log_msg("任务已完成")
                break

        return True
